<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CMS</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/jumbotron.css" rel="stylesheet">
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.min.js"></script>
    <script src="js/respond.min.js"></script>
    <![endif]-->
    <!--<link href="css/nav.css" rel="stylesheet">-->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top"></nav>

<div>
    <div class="container">
        <ul class="nav nav-tabs">
            <li class="active"><a href="javascript:reLoad();">推荐位</a></li>
        </ul>
    </div>

    <div class="container">
        &nbsp;
    </div>

    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <form class="form-inline form-all" >
                    <div class="form-group">
                        <button type="button" class="btn btn-link" data-toggle="modal" data-target="#newModal" data-ext="add" >添加推荐位</button>
                        <div class="input-group">
                            <div class="input-group-addon">标识</div>
                            <input type="text" class="form-control" id="tag" placeholder="标识" value="">
                        </div>
                        <div class="input-group">
                        	<div class="input-group-addon">年级</div>
	                        <select class="form-control" id="grade">
	                            <option value="">所有</option>
	                            <option value="1" >一年级</option>
	                            <option value="2" >二年级</option>
	                            <option value="3" >三年级</option>
	                            <option value="4" >四年级</option>
	                            <option value="5" >五年级</option>
	                            <option value="6" >六年级</option>
	                        </select>
	                    </div>
                    </div>
                    <button type="button" class="btn btn-primary" id="btn-sub">查询</button>
                </form>
            </div>
            <div class="table-responsive">
                <table class="table table-hover table-striped">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>类型</th>
                        <th>标识</th>
                        <th>年级</th>
                        <th class="col-md-2">推荐文字</th>
                        <th class="col-xs-4 col-lg-2">推荐图片</th>
                        <th>新增/最近修改时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="table-body"></tbody>
                </table>
            </div>
            <nav style="text-align: right">
                <ul class="pagination"></ul>
            </nav>
        </div>
    </div>

</div>


<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/url.min.js"></script>
<script src="js/common.js"></script>
<script language="JavaScript">

    $(function(){
        auth();
        menu("promotion");
        $(".container").filter(":last").after(newModal);
        var page = $.url('?page');
        var grade = $.url('?grade');
        var tag = $.url('?tag');
        var opts = {"page":(page != null)?page:1,"count":20}
        if(tag != null && tag != ""){
            opts.tag = tag;
            $("#tag").val(tag);
        }
        if(grade != null && grade != ""){
            opts.grade = grade;
            $("#grade").val(grade);
        }
        api.promotionList(opts);
        $('#newModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);
            var ext = button.data('ext');
            var recipient = button.data('whatever');
            var title = button.data('rowid');
            var src = "";
            switch(ext)
            {
                case "edit":
                    title = "#"+title;
                    src = "promotion_edit.html?id="+recipient;
                    break;
                case "add":
                    title = "添加推荐";
                    src = "promotion_edit.html";
                    break;
                case "add-img":
                    title = "添加图片 #"+title;
                    var img = button.data('img');
                    src = "promotion_upload.html?id="+recipient+"&img="+encodeURI(img);
                    break;
            }
            var modal = $(this);
            modal.find('.modal-title').text(title);
            $("#newiframe").attr("src", src);
        });
        $('#newModal').on('hide.bs.modal', function (event) {
            reLoad();
        });
        window.addEventListener("message", function( e ) {
            var j = JSON.parse(e.data);
            $('#newModal').modal('hide');
            if(j.hasOwnProperty("err") && j.err ==0){
                msg("图片上传成功！"+ j.url);
                reLoad();
            }else{
                msgErr("上传失败，仅支持jpg！");
            }
        }, false );
	    $("#btn-sub").click(function(){
//	      var opts = {};
	        var tag = $("#tag").val();
	        var grade = $("#grade").val();
	        opts.page = 1;
	        opts.tag = tag;
	        opts.grade = grade;
	        console.info(opts);
	      api.promotionList(opts);
	    });
    });
</script>
</body>
</html>